/*
 * Created by JFormDesigner on Wed May 19 10:37:56 CST 2021
 */

package renthouse.view.sever;

import java.awt.event.*;

import renthouse.entry.Admin;
import renthouse.entry.Landlord;
import renthouse.entry.User;
import renthouse.sever.LoGinSever;
import renthouse.util.CommenUtil;
import renthouse.view.mainManger.AdminFrame;
import renthouse.view.mainManger.LandlordFrame;
import renthouse.view.mainManger.UserFrame;

import java.awt.*;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class LoginFrame extends JFrame {
    /*运行可视化界面*/
    public static void main(String[] args) {
        LoginFrame login = new LoginFrame();
        login.setVisible(true);

    }
//存取登录信息用来判别相关判定
    public static Landlord loginlandlord = null;
    public static User loginuser=null;
    public static Admin loginadmin=null;



    public LoginFrame() {
        initComponents();
    }


    /*按钮获取用户登录的相关信息*/
    LoGinSever loGinSever = new LoGinSever();

    private void loginActionPerformed() {
        // TODO add your code here
        String statusVal = (String) status.getSelectedItem();
        String loginnameVal = loginname.getText();
        String passwordVal = password.getText();
        if (CommenUtil.isNull(loginnameVal)) {
            JOptionPane.showMessageDialog(null, "用户名为空");
            return;
        }
        if (CommenUtil.isNull(passwordVal)) {
            JOptionPane.showMessageDialog(null, "密码为空");
            return;
        }
        /*在不同下拉框值中获取不同窗口*/
        if (statusVal == "管理员") {
            boolean f = loGinSever.login_admin(loginnameVal, passwordVal);
            if (f) {
                setVisible(false);
                AdminFrame adminFrame = new AdminFrame();
                adminFrame.setVisible(true);//打开主窗口
                loginadmin.setLoginname(loginnameVal);
            } else {
                JOptionPane.showMessageDialog(null, "用户名或者密码错误");

            }
        } else if (statusVal == "房东") {
            boolean f = loGinSever.login_landlord(loginnameVal, passwordVal);
            if (f) {
                setVisible(false);
                LandlordFrame landlordFrame = new LandlordFrame();
                landlordFrame.setVisible(true);//打开主窗口
                loginlandlord.setLoginname(loginnameVal);
            } else {
                JOptionPane.showMessageDialog(null, "用户名或者密码错误");

            }

        } else if (statusVal == "房客") {
            boolean f = loGinSever.login_user(loginnameVal, passwordVal);
            if (f) {
                setVisible(false);
                UserFrame userFrame = new UserFrame();
                userFrame.setVisible(true);//打开主窗口
            } else {
                JOptionPane.showMessageDialog(null, "用户名或者密码错误");

            }
        }
    }


    private void registerActionPerformed(ActionEvent e) {
        // TODO add your code here
        setVisible(false); //关闭当前窗口
        RegisterFrame registerFrame = new RegisterFrame();
        registerFrame.setVisible(true);//打开主窗口
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel2 = new JPanel();
        label1 = new JLabel();
        panel3 = new JPanel();
        label2 = new JLabel();
        label3 = new JLabel();
        loginname = new JTextField();
        password = new JTextField();
        label4 = new JLabel();
        status = new JComboBox<>();
        panel1 = new JPanel();
        register = new JButton();
        login = new JButton();

        //======== this ========
        setTitle("\u767b\u5f55\u754c\u9762");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel2 ========
        {
            panel2.setLayout(null);

            //---- label1 ----
            label1.setText("\u5b9a\u5236\u5316\u79df\u623f\u7cfb\u7edf");
            label1.setHorizontalAlignment(SwingConstants.CENTER);
            label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 14f));
            panel2.add(label1);
            label1.setBounds(35, 5, 210, 45);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(50, 20, 305, 55);

        //======== panel3 ========
        {
            panel3.setLayout(null);

            //---- label2 ----
            label2.setText("\u8d26\u53f7\uff1a");
            label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 4f));
            panel3.add(label2);
            label2.setBounds(35, 65, 55, label2.getPreferredSize().height);

            //---- label3 ----
            label3.setText("\u5bc6\u7801\uff1a");
            label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 4f));
            panel3.add(label3);
            label3.setBounds(35, 100, 55, 22);
            panel3.add(loginname);
            loginname.setBounds(120, 65, 155, loginname.getPreferredSize().height);
            panel3.add(password);
            password.setBounds(120, 100, 155, 24);

            //---- label4 ----
            label4.setText("\u8eab\u4efd\uff1a");
            label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 4f));
            panel3.add(label4);
            label4.setBounds(35, 30, 55, 22);

            //---- status ----
            status.setModel(new DefaultComboBoxModel<>(new String[] {
                "\u7ba1\u7406\u5458",
                "\u623f\u4e1c",
                "\u623f\u5ba2"
            }));
            panel3.add(status);
            status.setBounds(120, 30, 155, status.getPreferredSize().height);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel3);
        panel3.setBounds(55, 95, 345, 145);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- register ----
            register.setText("\u6ce8\u518c");
            register.setFont(register.getFont().deriveFont(register.getFont().getSize() + 6f));
            register.setBackground(new Color(204, 204, 204));
            register.addActionListener(e -> registerActionPerformed(e));
            panel1.add(register);
            register.setBounds(35, 25, 130, 30);

            //---- login ----
            login.setText("\u767b\u5f55");
            login.setFont(login.getFont().deriveFont(login.getFont().getSize() + 6f));
            login.setBackground(new Color(204, 204, 204));
            login.addActionListener(e -> loginActionPerformed());
            panel1.add(login);
            login.setBounds(220, 25, 130, 30);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(25, 250, 395, 90);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel2;
    private JLabel label1;
    private JPanel panel3;
    private JLabel label2;
    private JLabel label3;
    private JTextField loginname;
    private JTextField password;
    private JLabel label4;
    private JComboBox<String> status;
    private JPanel panel1;
    private JButton register;
    private JButton login;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
